import java.util.HashMap;

public class Solution {
    public int findLHS(int[] nums) {
        HashMap<Integer, Integer> cnt = new HashMap<>();
        int res = 0;
        // 统计每个数字的出现次数
        for (int num : nums) {
            cnt.put(num, cnt.getOrDefault(num, 0) + 1);
        }
        // 遍历哈希表，检查每个数字的相邻数字是否存在
        for (int key : cnt.keySet()) {
            if (cnt.containsKey(key + 1)) {
                // 更新最长和谐子序列的长度
                res = Math.max(res, cnt.get(key) + cnt.get(key + 1));
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1, 3, 2, 2, 5, 2, 3, 7};
        System.out.println(solution.findLHS(nums)); // 输出应为 5 (2+3)
    }
}